package com.nt.train.service.impl;

import com.nt.train.entity.Dict;
import com.nt.train.service.DictService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @author hlf
 * @version 1.0
 * @date 2022/8/17 13:55
 */
@Service
public class DictServiceImpl implements DictService {

    private  static List<Dict> dbDictList = null;

    static {
        dbDictList = new ArrayList<Dict>();
        for(int i=1 ;i<= 50;i++){
            Dict dict  = new Dict();
            //主键
            dict.setId(Long.valueOf(i));
            //父级ID
            dict.setParentId(Long.valueOf(i-1));
            //字典名称
            dict.setName("红牛-"+i);
            // 类型
            dict.setType(1000+i);
            //字典码
            dict.setCode(""+(1000+i));
            //字典值
            dict.setValue(""+(2000+i));
            //状态
            dict.setStatus(1);
            //排序值
            dict.setSort(i);
            //备注
            dict.setRemark("演示demo");
            dbDictList.add(dict);
        }
    }


    @Override
    public List<Dict> listDictPage(Integer pageIndex, Integer pageSize, Dict dict) {
        //总数
        int total = dbDictList.size();
        //总页数
        int pageSum = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        //分页
        List<Dict> subList = dbDictList.stream().skip((pageIndex - 1) * pageSize).limit(pageSize).
                collect(Collectors.toList());
      //  subList = getPageList(pageIndex,pageSize,dbDictList);
        return subList;
    }


    /**
     * 传统分页处理
     * @param pageNum
     * @param pageSize
     * @param l
     * @return
     */
    private static List<Dict> getPageList(Integer pageNum, Integer pageSize, List<Dict> l) {
        int count = 0;
        int fromIndex = 0;
        int toIndex = 0;
        if (l != null && l.size() > 0) {
            count = l.size();
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = pageNum * pageSize;
            if (toIndex > count) {
                toIndex = count;
            }
        }
        List<Dict> pageList = l.subList(fromIndex, toIndex);
        return pageList;
    }



    @Override
    public List<Dict> selectDictTypeList(Dict dict) {
        List<Dict>  dictList = dbDictList.stream().filter(obj -> obj.getType().equals(dict.getType())).collect(Collectors.toList());
        return dictList;
    }

    @Override
    public List<Dict> selectDictAll() {
        return dbDictList;
    }

    @Override
    public List<Dict> listByParentId(Long parentId) {
        List<Dict>  dictList = dbDictList.stream().filter(obj -> obj.getParentId().equals(parentId)).collect(Collectors.toList());
        return dictList;
    }

    @Override
    public Dict selectDictById(Long dictId) {
        //实现查找，找到则返回匹配的对象，找不到则返回null
        Dict dictData = dbDictList.stream().filter(dict -> dict.getId().equals(dictId)) .findAny() .orElse(null);
        return dictData;
    }

    @Override
    public Dict getByCode(String code) {
        //实现查找，找到则返回匹配的对象，找不到则返回null
        Dict dictData = dbDictList.stream().filter(dict -> dict.getCode().equals(code)) .findAny() .orElse(null);
        return dictData;
    }

    @Override
    public void deleteDictById(Long dictId) {
       dbDictList.stream().findFirst().map(dict -> {
            if (dict.getId().equals(dictId)){
                dbDictList.remove(dict);
            }
            return dict;
        });
        //System.out.println(dbDictList.size());
    }

    @Override
    public void deleteDictByIds(List<Long> ids) {
        ids.forEach(dictId->{
            dbDictList.stream().findFirst().map(dict -> {
                if (dict.getId().equals(dictId)){
                    dbDictList.remove(dict);
                }
                return dict;
            });
        });
    }

    @Override
    public int insertDict(Dict dict) {
        dbDictList.add(dict);
        return 1;
    }

    @Override
    public int updateDict(Dict dict) {
        dbDictList.stream().filter(db->db.getId().equals(dict.getId()))
                .forEach(f ->
                        {
                            //主键
                            f.setId(Long.valueOf(dict.getId()));
                            //父级ID
                            f.setParentId(dict.getParentId());
                            //字典名称
                            f.setName(dict.getName());
                            // 类型
                            f.setType(dict.getType());
                            //字典码
                            f.setCode(dict.getCode());
                            //字典值
                            f.setValue(dict.getValue());
                            //状态
                            f.setStatus(1);
                            //排序值
                            f.setSort(dict.getSort());
                            //备注
                            f.setRemark(dict.getRemark());
                        }
                );
        return 1;
    }
}

